import UnityEngine
import System

class Lobby (MonoBehaviour): 
    polling as bool
    connecting as bool
    pollcount as int = 0
    def Start():
        MasterServer.ipAddress = "128.113.90.232" # gamedev.union.rpi.edu
        MasterServer.port = 12345
    
    def OnEnable ():
        MasterServer.UnregisterHost()
        polling = false
        connecting = false
    
    def Update ():
        if polling:
            list = MasterServer.PollHostList()
            pollcount = len(list)
            for host in list:
                Network.Connect(host.ip, host.port)
                connecting = true
                polling = false
    
    def OnGUI():
        if GUI.Button(Rect(0,Screen.height-50,200,50), "Create"):
            Network.InitializeServer(32, 18000)
        
        join as string
        if polling:
            join = String.Format("Polling ({0:G})", pollcount)
        elif connecting:
            join = "Connecting"
        else:
            join = "Join"
        
        if GUI.Button(Rect(Screen.width-200,Screen.height-50,200,50), join):
            if not polling:
                polling = true
                MasterServer.RequestHostList("EGD FPS TEST")
        
    
    def OnFailedToConnect():
        connecting = polling = false
    
    def OnServerInitialized():
        gameObject.GetComponent(Lobby).enabled = false
        gameObject.GetComponent(Manager).enabled = true
        MasterServer.RegisterHost("EGD FPS TEST", "Connect Test")
    
    def OnConnectedToServer():
        gameObject.GetComponent(Lobby).enabled = false
        gameObject.GetComponent(Manager).enabled = true
    
